$(function($) {

	
	$("#headerShowRegisterWindow").on("click", function(){
		$.showRegisterWindow();
		return false;
     });
	/* ---------- Header ---------- */
	
	$.showRegisterWindow=function () {
var registerWindowHtml = 
	' <form id="registerWindowForm"> '+
	'<table> <tr> <th>用户名: </th> <td> <input type="text" id="registerWindowMemberUsername" name="username" class="formText" title="用户名只允许包含中文、英文、数字和下划线!" /> </td> </tr>'+ 
	'<tr> <th>密&nbsp;&nbsp;&nbsp;码: </th> <td> <input type="password" id="registerWindowMemberPassword" name="password" class="formText" /> </td> </tr> '+
	'<tr> <th>重复密码: </th> <td> <input type="password" id="registerWindowReMemberPassword" name="reMemberPassword" class="formText" title="密码长度只允许在4-20之间!" /> </td> </tr> '+
	'<tr> <th>E-mail: </th> <td> <input type="text" id="registerWindowMemberEmail" name="email" class="formText" /> </td> </tr>'+
	'<tr> <th>联系电话: </th> <td> <input type="text" id="telephone" name="telephone" class="formText" /> </td> </tr>'+
	'</table> </form>';			
			
     $.dialog({title: "会员注册", content: registerWindowHtml, ok: "注 册", cancel: "取 消", id: "registerWindow", className: "registerWindow", width: 480, okCallback: register, modal: true});
     }	;
//			var $("#registerWindowForm") = $("#registerWindowForm");
//			var registerWindowMemberUsername = $("#registerWindowMemberUsername");
//			var $("#registerWindowMemberPassword") = $("#registerWindowMemberPassword");
//			var $("#registerWindowReMemberPassword") = $("#registerWindowReMemberPassword");
//			var $("#registerWindowMemberEmail") = $("#registerWindowMemberEmail");

			function register() {
//				alert();
				if ($("#registerWindowMemberUsername").val() == "") {
					$("#registerWindowMemberUsername").focus();
					$.message({type: "warn", content: "请输入用户名!"});
					return false;
				}
				if (!/^[\u0391-\uFFE5\w]+$/.test($("#registerWindowMemberUsername").val())) {
					$("#registerWindowMemberUsername").focus();
					$.message({type: "warn", content: "用户名只允许包含中文、英文、数字和下划线!"});
					return false;
				}
				if ($.trim($("#registerWindowMemberUsername").val()).length < 2 || $.trim($("#registerWindowMemberUsername").val()).length > 20) {
					$("#registerWindowMemberUsername").focus();
					$.message({type: "warn", content: "用户名长度只允许在2-20之间!"});
					return false;
				}
				if ($.trim($("#registerWindowMemberPassword").val()) == "") {
					$("#registerWindowMemberPassword").focus();
					$.message({type: "warn", content: "请输入密码!"});
					return false;
				}
				if ($.trim($("#registerWindowMemberPassword").val()).length < 4 || $.trim($("#registerWindowMemberPassword").val()).length > 20) {
					$("#registerWindowMemberPassword").focus();
					$.message({type: "warn", content: "密码长度只允许在4-20之间!"});
					return false;
				}
				if ($.trim($("#registerWindowReMemberPassword").val()) == "") {
					$("#registerWindowReMemberPassword").focus();
					$.message({type: "warn", content: "请输入重复密码!"});
					return false;
				}
				if ($.trim($("#registerWindowReMemberPassword").val()) != $.trim($("#registerWindowMemberPassword").val())) {
					$("#registerWindowReMemberPassword").focus();
					$.message({type: "warn", content: "两次密码输入不相同!"});
					return false;
				}
				if ($.trim($("#registerWindowMemberEmail").val()) == "") {
					$("#registerWindowMemberEmail").focus();
					$.message({type: "warn", content: "请输入E-mail!"});
					return false;
				}
				if (!/^([a-zA-Z0-9._-])+@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-])+/.test($("#registerWindowMemberEmail").val())) {
					$("#registerWindowMemberEmail").focus();
					$.message({type: "warn", content: "E-mail格式错误!"});
					return false;
				}
				
				$.ajax({
					url: "http://localhost/register",
					data: $("#registerWindowForm").serialize(),
					type: "POST",
					dataType: "json",
					cache: false,
					success: function(data) {
						if (data.status == "success") {
							$.closeDialog("registerWindow");
							$.message({type: "success", content: "注册成功!"});
						}
						else{
							$("#registerWindowReMemberPassword").focus();
							$.message({type: "warn", content: "用户名已存在,请重新输入!"});
						}
					},
					complete: function() {
						$("#registerWindowForm").find("button").attr("disabled", false);
					}
				});				
				
			}

});